<!--
 Copyright 2023 The CubeFS Authors.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 implied. See the License for the specific language governing
 permissions and limitations under the License.
-->

<template>
  <section class="bucket_detail">
    <el-card>
      <ModuleBucketTitle :go-back="goBack" :bucket="volName">
        <span>{{ $t('filemanage.whichcluster') }} ：<span class="value-c">{{ clusterName }}</span></span>
      </ModuleBucketTitle>
      <el-alert
        :title="$t('common.tips')"
        type="info"
        show-icon>
        <div>
          <p>{{ $t('filemanage.tips1') }}</p>
          <p>{{ $t('filemanage.source') }}: {{ protocol }}//{{ host }}</p>
          <p>{{ $t('filemanage.allowmethods') }}: PUT,GET,POST,DELETE</p>
          <p>{{ $t('filemanage.allowheaders') }}: *</p>
        </div>
      </el-alert>
      <el-tabs v-model="activeName">
        <el-tab-pane :label="$t('filemanage.corscfg')" name="cors"></el-tab-pane>
        <el-tab-pane :label="$t('filemanage.filemg')" name="file" :disabled="disabledFile"></el-tab-pane>
      </el-tabs>
      <file v-if="activeName === 'file'"></file>
      <Cors v-if="activeName === 'cors'" @get-cors="getCors"></Cors>
    </el-card>
  </section>
</template>

<script>
import ModuleBucketTitle from './components/moduleBucketTitle'
import mixin from '@/pages/cfs/clusterOverview/mixin'
import File from './file'
import Cors from './Cors/Index.vue'
export default {
  components: {
    ModuleBucketTitle, File, Cors
  },
  mixins: [mixin],
  data() {
    return {
      id: '',
      zone: '',
      activeName: 'cors',
      volName: '',
      corsRules: [],
      disabledFile: true,
    }
  },
  computed: {
    host() {
      return window.location.host
    },
    protocol() {
      return window.location.protocol
    },
  },
  watch: {
    corsRules: {
      handler(val) {
        const { host, protocol } = window.location
        const ruleItem = val.find(item => item.AllowedOrigins.find(origins => origins.includes(`${protocol}//${host}`)))
        if (ruleItem) {
          this.disabledFile = false
        } else {
          this.disabledFile = true
        }
      },
      deep: true,
    },
  },
  created() {
    this.volName = this.$route.query.name
  },
  methods: {
    goBack() {
      this.$router.push({ name: 'fileManageList' })
    },
    getCors(corsRules) {
      this.corsRules = corsRules
    },
  },
}
</script>

<style lang="scss" scoped>
.bucket_detail {
  h2 {
    padding-left: 4px;
    border-left: 4px solid #2fc29b;
    margin-bottom: 16px;
  }
  .bucket_msg {
    color: #999999;
    margin-bottom: 40px;
    span {
      margin-right: 20px;
      font-size: 14px;
      .value-c {
        color: #666666;
        margin-right: 0;
      }
    }
  }
}
::v-deep .el-card {
  margin-bottom: 16px;
  border: 1px solid #f6f6f6;
}
</style>
